package com.project.service;

import com.alibaba.fastjson.JSONObject;
import com.project.dao.SchoolMapper;
import com.project.entity.PositionType;
import com.project.entity.School;
import com.project.entity.StudentInfo;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/26 15:02
 * @param:
 * @return:
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class SchoolServiceImpl extends BaseService implements SchoolService {
    @Autowired
    private  SchoolMapper schoolMapper;
    @Autowired
    private SchoolMangerService schoolMangerService;
    @Autowired
    private StudentInfoService studentInfoService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private ClassService classService;
    @Autowired
    private PositionTypeService positionTypeService;


    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 增加学院
     *
     * @param school
     * @return
     */
    @Override
    public int add(School school) {
        schoolMapper.insertSelective(school);

        //新建时预先插入岗位类别
        PositionType positionType = new PositionType();
        positionType.setSchoolid(school.getId());
        positionType.setTypename("其他");
        positionTypeService.addOne(positionType);

        return 1;
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据id删除学院
     *
     * @param id
     * @return
     */
    @Override
    public int remove(Integer id) {
        return schoolMapper.deleteByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据id修改学院信息
     *
     * @param school
     * @return
     */
    @Override
    public int modify(School school) {
        return schoolMapper.updateByPrimaryKeySelective(school);

    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据id寻找学院信息
     *
     * @param id
     * @return
     */
    @Override
    public School findOne(Integer id) {
        return schoolMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据universityid寻找学院信息以及学院管理员
     *
     * @param universityid
     * @return
     */
    @Override
    public List<JSONObject> findBatchWithAdminByUniversityId(Integer universityid) throws Exception {
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<School> schoolList = schoolMapper.selectByUniversityId(universityid);
        for (School school : schoolList) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("school", school);
            jsonObject.put("schoolManager", schoolMangerService.findOneBySchoolId(school.getId()));
            jsonObjectList.add(jsonObject);
        }
        return jsonObjectList;
    }

    /**
     * author: WheelChen
     * date: 2017-5-4
     * function: 根据universityid寻找学院信息
     *
     * @param universityid
     * @return
     */
    @Override
    public List<School> findBatchByUniversityId(Integer universityid) {
        return schoolMapper.selectByUniversityId(universityid);
    }


    /**
     * author: WheelChen
     * date: 2017/7/11
     * function: 检查是否已存在同一所学校相同学院
     *
     * @param school
     * @return true-存在 false-不存在
     */
    @Override
    public boolean checkUniversityIdAndNameIsExist(School school) {
        return schoolMapper.checkUniversityIdAndNameIsExist(school) > 0;
    }

    /**
     * author: WheelChen
     * date: 2017/10/30
     * function: 删除学院及学院下的所有学生信息以及学院管理员信息
     *
     * @param schoolId 学院id
     * @param request
     * @return
     */
    @Override
    public boolean removeOneChain(Integer schoolId, HttpServletRequest request) {
        //删除学院表信息
        remove(schoolId);
        //删除系
        departmentService.removeBatchBySchoolId(schoolId);
        //删除专业
        majorService.removeBatchBySchoolId(schoolId);
        //删除班级
        classService.removeBatchBySchoolId(schoolId);
        //删除学院管理员
        schoolMangerService.removeOneBySchoolId(schoolId);
        //查出班级下的学生表信息
        List<StudentInfo> studentInfoList = studentInfoService.findBatchBySchoolId(schoolId);
        if (studentInfoList.size() != 0)
                studentService.removeBatchChain(studentInfoList);
        return true;
}


}
